package mi.payment.admin.pay88;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Set;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.ui.base.ParentActivity;

public class IPay88WalletActivity extends ParentActivity {

    private LinearLayout mContainerView;
    private IPay88WalletDialog mIPay88WalletDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_pay88_wallet_activity);
        initView();
        initData();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.pay88_setting_wallet);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );
        mContainerView = findViewById(R.id.view_container);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissIPay88WalletDialog();
    }

    private void initData() {
        HashMap<String, IPay88Wallet> map = IPay88Operation.getInstance().getIPay88WalletMap();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            IPay88Wallet wallet = map.get(key);
            View view = View.inflate(mContext, R.layout.ui_item, null);
            view.findViewById(R.id.right_text).setVisibility(View.INVISIBLE);
            TextView leftText = view.findViewById(R.id.left_text);
            ImageView leftImage = view.findViewById(R.id.left_image);

            leftText.setText(wallet.name);
            leftImage.setImageResource(wallet.iconResId);
            view.setOnClickListener(
                    v -> showIPay88WalletDialog(wallet)
            );
            mContainerView.addView(view);
        }
    }

    private void showIPay88WalletDialog(IPay88Wallet wallet) {
        try {
            mIPay88WalletDialog = new IPay88WalletDialog(this, wallet);
            mIPay88WalletDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void dismissIPay88WalletDialog() {
        if (mIPay88WalletDialog != null) {
            try {
                mIPay88WalletDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
